// 格式化数字
function formatNumber(number, decimals = 2) {
    if (number === null || number === undefined || isNaN(number)) {
        return '-';
    }
    return Number(number).toLocaleString('zh-CN', {
        minimumFractionDigits: decimals,
        maximumFractionDigits: decimals
    });
}

// 格式化日期
function formatDate(date) {
    if (!date) return '-';
    const d = new Date(date);
    if (isNaN(d.getTime())) return '-';
    return d.toLocaleDateString('zh-CN');
}

// 防抖函数
function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
        const later = () => {
            clearTimeout(timeout);
            func(...args);
        };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
    };
}

// 节流函数
function throttle(func, limit) {
    let inThrottle;
    return function executedFunction(...args) {
        if (!inThrottle) {
            func(...args);
            inThrottle = true;
            setTimeout(() => inThrottle = false, limit);
        }
    };
}

// 表格排序
function sortTable(table, column, type = 'string') {
    const tbody = table.querySelector('tbody');
    const rows = Array.from(tbody.querySelectorAll('tr'));
    const sortedRows = rows.sort((a, b) => {
        const aCol = a.children[column];
        const bCol = b.children[column];
        const aValue = aCol.textContent.trim();
        const bValue = bCol.textContent.trim();

        if (type === 'number') {
            return parseFloat(aValue) - parseFloat(bValue);
        }
        if (type === 'date') {
            return new Date(aValue) - new Date(bValue);
        }
        return aValue.localeCompare(bValue, 'zh-CN');
    });

    while (tbody.firstChild) {
        tbody.removeChild(tbody.firstChild);
    }
    tbody.append(...sortedRows);
}

// 表格搜索过滤
function filterTable(table, searchText) {
    const rows = table.querySelectorAll('tbody tr');
    rows.forEach(row => {
        const text = row.textContent.toLowerCase();
        const match = text.includes(searchText.toLowerCase());
        row.style.display = match ? '' : 'none';
    });
}

// 创建分页控件
function createPagination(total, current, pageSize, onChange) {
    const container = document.createElement('div');
    container.className = 'pagination';
    container.style.cssText = `
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        margin: 16px 0;
    `;

    const totalPages = Math.ceil(total / pageSize);

    // 首页按钮
    const firstBtn = document.createElement('button');
    firstBtn.textContent = '首页';
    firstBtn.disabled = current === 1;
    firstBtn.onclick = () => onChange(1);

    // 上一页按钮
    const prevBtn = document.createElement('button');
    prevBtn.textContent = '上一页';
    prevBtn.disabled = current === 1;
    prevBtn.onclick = () => onChange(current - 1);

    // 页码显示
    const pageInfo = document.createElement('span');
    pageInfo.textContent = `${current}/${totalPages}页`;

    // 下一页按钮
    const nextBtn = document.createElement('button');
    nextBtn.textContent = '下一页';
    nextBtn.disabled = current === totalPages;
    nextBtn.onclick = () => onChange(current + 1);

    // 末页按钮
    const lastBtn = document.createElement('button');
    lastBtn.textContent = '末页';
    lastBtn.disabled = current === totalPages;
    lastBtn.onclick = () => onChange(totalPages);

    // 总数显示
    const totalInfo = document.createElement('span');
    totalInfo.textContent = `共${total}条`;

    container.append(firstBtn, prevBtn, pageInfo, nextBtn, lastBtn, totalInfo);
    return container;
}

// 导出工具函数
window.Utils = {
    formatNumber,
    formatDate,
    debounce,
    throttle,
    sortTable,
    filterTable,
    createPagination
}; 